package com.bjesc_b2c.app.appraiset.mvp.view.activity;

import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bjesc_b2c.app.appraiset.R;
import com.bjesc_b2c.app.appraiset.base.BaseActivity;
import com.bjesc_b2c.app.appraiset.base.BaseIntentionFragment;
import com.bjesc_b2c.app.appraiset.bean.WaitTrackBean;
import com.bjesc_b2c.app.appraiset.mvp.view.fragment.Inte_FollowClueFragment;
import com.bjesc_b2c.app.appraiset.mvp.view.fragment.Inte_ForwardClueFragment;
import com.bjesc_b2c.app.appraiset.mvp.view.fragment.IntentionAfterTomorrowFragment;
import com.bjesc_b2c.app.appraiset.mvp.view.fragment.IntentionTodayFragment;
import com.bjesc_b2c.app.appraiset.mvp.view.fragment.IntentionTomorrowFragment;
import com.bjesc_b2c.app.appraiset.widget.popupwindow.TrackPop;
import com.example.codeutils.utils.TimeUtils;
import com.ogaclejapan.smarttablayout.SmartTabLayout;
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItemAdapter;
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItems;
import com.rxretrofit_build.model.RxBusBean;

import java.util.Date;

import butterknife.Bind;
import butterknife.OnClick;
import rxbus_base.RxBus;

/**
 * Created by yidong on 2017/8/8.
 */

public class IntentionTouristActivity extends BaseActivity {
    @Bind(R.id.viewpager)
    ViewPager mViewPager;
    @Bind(R.id.viewpager_tab)
    SmartTabLayout mViewpagerTab;
    @Bind(R.id.title_view)
    View titleView;
    @Bind(R.id.tv_title)
    TextView titleTv;
    @Bind(R.id.iv_return)
    LinearLayout returnLin;
    @Bind(R.id.tv_car_name_title)
    TextView nameTv;
    @Bind(R.id.edit_search_name)
    EditText searchEdit;
    private TrackPop trackPop;
    private Intent intent = new Intent();
    private int type = 1;

    @Override
    protected int getLayout() {
        return R.layout.activity_await_task;
    }

    @Override
    protected void initEventAndData() {
        returnLin.setVisibility(View.VISIBLE);
        titleTv.setText("意向客源待跟进");

        final LayoutInflater inflater = LayoutInflater.from(this);
        String week1 = TimeUtils.getWeek(TimeUtils.getDayOfTomorrow(new Date(),1));
        String week2 = TimeUtils.getWeek(TimeUtils.getDayOfTomorrow(new Date(),2));
        final String[] tabTitles = {"过去","今天",week1.replace("星期","周"),week2.replace("星期","周"),"将来"};
        FragmentPagerItems pages = FragmentPagerItems.with(this)
//        0过期 1今天 2明天 3后天 -1将来 默认1
                .add(tabTitles[0], Inte_FollowClueFragment.class)
                .add(tabTitles[1], IntentionTodayFragment.class)
                .add(tabTitles[2], IntentionTomorrowFragment.class)
                .add(tabTitles[3], IntentionAfterTomorrowFragment.class)
                .add(tabTitles[4], Inte_ForwardClueFragment.class)
                .create();
        FragmentPagerItemAdapter adapter = new FragmentPagerItemAdapter(getSupportFragmentManager(), pages);
        mViewPager.setOffscreenPageLimit(pages.size());
        mViewPager.setAdapter(adapter);
        mViewpagerTab.setCustomTabView(new SmartTabLayout.TabProvider() {
            @Override
            public View createTabView(ViewGroup container, int position, PagerAdapter adapter) {
                View view = inflater.inflate(R.layout.layout_navigation_top_item, container, false);
                TextView titleView = (TextView) view.findViewById(R.id.txt_title);
                titleView.setText(tabTitles[position % tabTitles.length]);
                return view;
            }
        });
        mViewPager.setCurrentItem(1);
        mViewpagerTab.setViewPager(mViewPager);
        searchEdit.addTextChangedListener(mTextWatcher);
        getIntentFuzzyQueryA();

    }

    @Override
    protected void createPresenter() {

    }

    @OnClick({R.id.iv_return, R.id.tv_car_name_title})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_return:
                finish();
                break;
            case R.id.tv_car_name_title:
                trackPop = new TrackPop(this, popListener);
                trackPop.showAsDropDown(titleView, 40, 20);
                break;
        }
    }

    private View.OnClickListener popListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            switch (view.getId()) {
                case R.id.tv_client_name:
                    nameTv.setText("客户名");
                    searchEdit.setHint("请输入客户名称");
                    type = 1;
                    searchEdit.setText("");
                    break;
                case R.id.tv_car_name:
                    nameTv.setText("主车");
                    searchEdit.setHint("请输入品牌/车系");
                    searchEdit.setText("");
                    type = 2;
                    break;
                case R.id.tv_order_id:
                    searchEdit.setText("");
                    nameTv.setText("订单号");
                    type = 3;
                    searchEdit.setHint("请输入订单号");
                    break;

            }
            if (trackPop.isShowing()) {
                trackPop.dismiss();
            }
        }
    };


    public void getIntentFuzzyQueryA() {

        searchEdit.setOnKeyListener(new View.OnKeyListener() {//输入完后按键盘上的搜索建
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                //判断是否按下。
                if (event.getAction() == KeyEvent.ACTION_DOWN) {
                    //判断是否是回车键
                    if (keyCode == event.KEYCODE_ENTER) {
                        String keyWord = searchEdit.getText().toString();
                        //为NULL 判断
                        if (!TextUtils.isEmpty(keyWord)) {
                            intent.putExtra("type", type);
                            intent.putExtra("keyWord", keyWord);
                            RxBus.getInstance().sendData(IntentionTouristActivity.class.getName(), intent);
                        }
                    }

                }
                return false;
            }

        });
    }

    TextWatcher mTextWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            RxBus.getInstance().sendData("keyWord", searchEdit.getText().toString());
        }
    };
    @Override
    protected void doOnNext(RxBusBean rxBusBean) {
        super.doOnNext(rxBusBean);
        if (rxBusBean.getRequestMark().equals(BaseIntentionFragment.class.getName())){
            if (rxBusBean.getO() instanceof WaitTrackBean.TotalBean){
                WaitTrackBean.TotalBean bean = (WaitTrackBean.TotalBean)rxBusBean.getO();
                TextView textView_1 = (TextView) mViewpagerTab.getTabAt(0).findViewById(R.id.txt_number);
                textView_1.setText(bean.getOld_count());
                textView_1.setTextColor(getResources().getColor(R.color.colorFFF7041B));
                TextView textView_2 = (TextView) mViewpagerTab.getTabAt(1).findViewById(R.id.txt_number);
                textView_2.setText(bean.getToday_count());
                textView_2.setTextColor(getResources().getColor(R.color.colorFFB047));
                TextView textView_3 = (TextView) mViewpagerTab.getTabAt(2).findViewById(R.id.txt_number);
                textView_3.setText(bean.getTomorrow_count());
                textView_3.setTextColor(getResources().getColor(R.color.colorFFB047));
                TextView textView_4 = (TextView) mViewpagerTab.getTabAt(3).findViewById(R.id.txt_number);
                textView_4.setText(bean.getAftertomorrow_count());
                textView_4.setTextColor(getResources().getColor(R.color.colorFFB047));
                TextView textView_5 = (TextView) mViewpagerTab.getTabAt(4).findViewById(R.id.txt_number);
                textView_5.setText(bean.getFuture_count());
                textView_5.setTextColor(getResources().getColor(R.color.colorFF9B9B9B));
            }
        }
    }
}